import { createContext, Dispatch } from "react";

export interface UserData {
  is_leader: 1 | 0 | undefined; //1组长/部长    0组员;
  department_name: string;
}

export const initData = {} as UserData;

type reduceType = "user" | "xxx";

interface ActionType {
  type: reduceType;
  payload: any;
}

export interface AppReducer {
  dispatch: Dispatch<ActionType>;
  data: any;
}
/**
 *
 * @param state 原来的数据（上一次的）
 * @param payLoad  新的data
 * @returns 返回UserData
 */
export const reducer = (state: UserData, payLoad: ActionType): UserData => {
  switch (payLoad.type) {
    case "user":
      return {
        ...state,
        ...payLoad.payload,
      };

    default:
      return initData;
  }
};

export const AppContext = createContext({} as AppReducer);
